﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using MixScriptWeb.control;
using MixScriptWeb.Share;
using MixScript.BLL;

namespace MixScriptWeb.Warehouse
{
    public partial class fInitStok : cCommonWeb, IMIxCommon
    {
        private static DataTable dt;
        private static SortDirection sd;
        private static cReferences oReff;
        private decimal dcQty = 0;

        // private static Control[] ctl;

        protected void Page_Load(object sender, EventArgs e)
        {
            try
            {
                if (!Page.IsPostBack)
                {
                    //dFindClickAttribute(this, new MixScriptTB[] { txtMaterialID, txtwrhcode, txtUOM }, new string[] { "SRCMAT", "SRCWRHID", "SRCUOM" });

                    //txtItNo.ReadOnly = true;
                    //  setSortExpression(gvdetail, new string[] { "banknum", "bankname", "address", "city", "country", "ac" });
                    //txtMaterialID.Tbx.Attributes.Add("readonly", "readonly");
                    //txtwrhcode.Tbx.Attributes.Add("readonly", "readonly");
                    //txtUOM.Tbx.Attributes.Add("readonly", "readonly");
                    string[] sheadertext = new string[] { "Unit","Material ID", "Warehouse ID","Warehouse" ,"Mat Name"};
                    string[] sdatavalue = new string[] { "usms", "itno", "wrhid", "wrhname" ,"itnm"};
                    initGvColumn(gvdetail, sheadertext, sdatavalue);
                    FillDropDownList(ddlModule, "vw_joid_module_v", "bpidnm", "codfbpid", true);
                    FillDropDownList(ddlWRHID, "tb_wrhmst", "WRHNAME", "WRHID", false);

                }
                mn = (MainMenu)this.Master;
                hideStatusBar();
                // oReff = new cReferences();
                // if(gvdetail.Rows.Count>0)


            }
            catch (Exception ex)
            {

                ShowStatusBar(ex.Message, msgType.msgError);
            }


        }

        protected void gvdetail_SelectedIndexChanged(object sender, EventArgs e)
        {
            try
            {

                //oUM = new cUserManagement();

                //if (gvdetail.SelectedIndex > -1)
                //{
                //    txtMaterialID.Text = gvdetail.SelectedRow.Cells[1].Text;
                //    txtwrhcode.Text = gvdetail.SelectedRow.Cells[2].Text;
                //    txtUOM.Text = gvdetail.SelectedRow.Cells[3].Text;
                //    txxQTY.Text = gvdetail.SelectedRow.Cells[4].Text;




                //    //ini ngehapus &nbsp;
                //    removeNbsp(new MixScriptTB[] { txtMaterialID, txtwrhcode, txtUOM, txxQTY });



                //}
            }
            catch (Exception ex)
            {

                ShowStatusBar(ex.Message);
            }
        }

        protected void gvdetail_Sorting(object sender, GridViewSortEventArgs e)
        {
            if (sd == SortDirection.Ascending)
            {
                sd = SortDirection.Descending;
            }
            else
            {
                sd = SortDirection.Ascending;
            }

            DataView dataView = new DataView(dt);
            dataView.Sort = e.SortExpression + " " + ShortDirection(sd);

            bindGridview(gvdetail, dataView);
        }

        protected void gvdetail_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {

            gvdetail.PageIndex = e.NewPageIndex;
            //gvdetail.DataBind();
            bindGridview(gvdetail, dt);


        }

        protected void btnFind_Click(object sender, EventArgs e)
        {
            try
            {
                //oReff = new cReferences(); //new cUserManagement();
                //dt = new DataTable();
                //Control[] ctl = new Control[] { txtMaterialID, txtwrhcode, txtUOM};
                //if (oReff.getInquiry(ref dt, genInqParam(ctl), "tb_initstok", out errMsg))
                //{

                //}
                //else
                //{
                //    ShowStatusBar("Record Not Found", msgType.msgError);
                //}
                //bindGridview(gvdetail, dt, lbDelete);


                oReff = new cReferences();

                //oReff.getInquiry(ref dt, new string[] { "", "",  "", "", "", "","", "", "","", "", "", "", "", ddlModule.SelectedValue,"",ddlWRHID.SelectedValue }, "vw_allparts_modul_v", out errMsg);
                oReff.getInquiryAllPartsModul(ref dt, txtMatname.Text, ddlWRHID.SelectedValue, ddlModule.SelectedValue);
                bindGridview(gvdetail, dt);
                if (dt.Rows.Count > 0)
                {
                    btnSave.Visible = true;
                }
                else
                {
                    btnSave.Visible = false;
                }




            }
            catch (Exception ex)
            {

                ShowStatusBar(ex.Message, msgType.msgError);
            }

        }

        protected void btnSave_Click(object sender, EventArgs e)
        {
            try
            {
                foreach (GridViewRow gr in gvdetail.Rows)
                {
                    TextBox tb = (TextBox)gr.FindControl("txtqty");
                    if(tb.Text.Trim()!=string.Empty)
                    {
                        decimal dqty=0;
                        if (decimal.TryParse(tb.Text, out dqty))
                        {
                            oReff = new cReferences();
                            if (!oReff.isRecordExist("tb_initstok", new string[] { "matid", "wrhid"}, new object[] {gr.Cells[2].Text,gr.Cells[3].Text}))
                            {
                                if (oReff.AddNewRowDB(out errMsg, "tb_initstok", new int[] { }, new string[] { }, false, gr.Cells[2].Text, gr.Cells[3].Text, gr.Cells[1].Text, DateTime.Now, "[SKIP]", "[SKIP]", dqty))
                                {
                                    ShowStatusBar(errMsg, msgType.msgSuccess);
                                    //  txtwrhid.Text = ss;
                                  //  btnFind_Click(btnFind, new EventArgs());
                                    
                                }
                                else
                                {
                                    ShowStatusBar(errMsg, msgType.msgError);
                                }

                            }
                            else
                            {

                                if (oReff.EditRowDtDB(out errMsg, "tb_initstok", new string[] { "matid", "wrhid" }, new object[] { gr.Cells[2].Text, gr.Cells[3].Text }, false, gr.Cells[2].Text, gr.Cells[3].Text, gr.Cells[1].Text, DateTime.Now, "[SKIP]", "[SKIP]", dqty))
                                {
                                    ShowStatusBar(errMsg, msgType.msgSuccess);
                                   
                                   
                                }
                                else
                                {
                                    ShowStatusBar(errMsg, msgType.msgError);
                                }

                               
                            }                            
                        }
                        
                    }
                }
                
            }
            catch (Exception ex)
            {

                ShowStatusBar(ex.Message);
            }
        }



        protected void btnDelete_Click(object sender, EventArgs e)
        {
            try
            {
                //cek apakah user sudah pilih data yg akan dihapus
                //if (txtMaterialID.Text != string.Empty)
                //{
                //    //cek apakah data itu ada
                //    if (oReff.isRecordExist("tb_initstok", new string[] { "matid","wrhid","uom" }, new object[] { txtMaterialID.Text ,txtwrhcode.Text,txtUOM.Text}))
                //    {
                //        oReff = new cReferences();

                //        //coba hapus
                //        //                                nama primary key                nilainya          nama tablenya   pesan error
                //        if (oReff.deleteRowDBSQL(new string[] { "matid", "wrhid", "uom" }, new object[] { txtMaterialID.Text, txtwrhcode.Text, txtUOM.Text }, "tb_initstok", out errMsg))
                //        {
                //            //jika berhasil
                //            ShowStatusBar(em.getError(8), msgType.msgSuccess);
                //        }
                //        else
                //        {   //jika gagal
                //            ShowStatusBar(errMsg);
                //        }

                //    }
                //    else
                //    {   //jika data tidak ditemukan
                //        ShowStatusBar(em.getError(7));
                //    }
                //}
                //else
                //{
                //    ShowStatusBar(em.getError(7));
                //}
            }
            catch (Exception ex)
            {

                ShowStatusBar(ex.Message);
            }
        }

        protected void btnClear_Click(object sender, EventArgs e)
        {
            try
            {
                errMsg = "";
                //setValid(new MixScriptTB[] { txtMaterialID, txtwrhcode, txtUOM, txxQTY });
                //clearMixTB(new MixScriptTB[] { txtMaterialID, txtwrhcode, txtUOM, txxQTY });
            }
            catch (Exception ex)
            {

                ShowStatusBar(ex.Message);
            }

        }



        #region IMIxCommon Members

        //validasi inputan
        public bool isvalid()
        {
            try
            {
            //    errMsg = "";
            //    //set semua control valid
            //    setValid(new MixScriptTB[] { txtMaterialID, txtwrhcode, txtUOM, txxQTY });
            //    //cek semua field mandatory
            //    if (MustFilledOk(new Control[] { txtMaterialID, txtwrhcode, txtUOM, txxQTY }, out errMsg))
            //    {
            //        if (!decimal.TryParse(txxQTY.Text, out dcQty))
            //        {

            //            ShowStatusBar(errMsg + " " + em.getError(10));
            //            return false;
            //        }

            //    }
            //    else
            //    {
            //        ShowStatusBar(errMsg + " " + em.getError(2));
            //        return false;
            //    }
                return true;
            }
            catch (Exception ex)
            {

                throw ex;
            }

        }

        #endregion




        #region Button





        #endregion


        #region Gridview
        #region Gridview Event





        protected void gvdetail_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            try
            {
                if (e.Row.DataItemIndex == -1)
                    return;
                e.Row.Attributes.Add("onMouseOver", "this.style.cursor='pointer';");
                e.Row.Attributes.Add("onclick", this.GetPostBackClientEvent(gvdetail, "Select$" + e.Row.RowIndex.ToString()));
                //e.Row.Attributes.Add("onclick", this.GetPostBackEventReference(gvdetail, "Select$" + e.Row.RowIndex.ToString())); //  (gvdetail, "Select$" + e.Row.RowIndex.ToString()));


            }
            catch (Exception ex)
            {

                ShowStatusBar(ex.Message);
            }
        }



        #endregion
        protected void cbAll_CheckedChanged(object sender, EventArgs e)
        {
            try
            {
                bool b = ((CheckBox)sender).Checked;


                foreach (GridViewRow gr in gvdetail.Rows)
                {
                    CheckBox cb = (CheckBox)gr.Cells[0].FindControl("cbitem");
                    cb.Checked = b;
                }

            }
            catch (Exception ex)
            {

                ShowStatusBar(ex.Message);
            }
        }

        protected void lbDelete_Click(object sender, EventArgs e)
        {
            try
            {
                oReff = new cReferences();
                bool isok = true;
                int i = 0;
                foreach (GridViewRow gr in gvdetail.Rows)
                {

                    if (((CheckBox)gr.Cells[0].FindControl("cbitem")).Checked)
                    {
                        string sid = gr.Cells[1].Text;

                        if (!oReff.deleteRowDB(new string[] { "wrhid" }, new string[] { sid }, "tb_wrhmst", out errMsg))
                        {
                            ShowStatusBar(em.getError(9) + sid + " " + errMsg);
                            isok = false;
                            continue;
                        }
                        else
                        {
                            i++;
                        }
                    }

                }

                if (i == 0 && isok)
                {
                    ShowStatusBar("Please Select Item First");
                }
                else if (isok)
                {
                    btnClear_Click(this, new EventArgs());
                    btnFind_Click(this, new EventArgs());
                    ShowStatusBar("Delete Completed", msgType.msgSuccess);
                }
            }
            catch (Exception ex)
            {

                ShowStatusBar(ex.Message);
            }
        }

        #endregion

        protected void ddlModule_SelectedIndexChanged(object sender, EventArgs e)
        {
            try
            {
                
               
            }
            catch (Exception ex)
            {

                ShowStatusBar(ex.Message);
            }
        }
    }
}